
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限列表管理</title>
    {include file="include/top"}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <?php
            $addShow='block';
            if($gid>0){
                $addShow='none';
            }
        ?>

        <div class="layui-card-header layuiadmin-card-header-auto" style="display: {$addShow};">
            <button class="layui-btn layuiadmin-btn-tags" data-type="add">添加分类</button>
        </div>
        <div class="layui-card-body">
            <div id="test12" class="demo-tree-more"></div>
            <div  id="authlist" class="demo-tree-more"></div>
        </div>
    </div>
</div>
{include file="include/js"}

<script>
    var pathid=0;
    var gid= {$gid};
    layui.config({
        base: '../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table'], function(){
        var table = layui.table;
        var $ = layui.$, active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加'
                    ,content: '/classes/tagsform?ty=1&pathid='+pathid
                    ,area: ['80%', '80%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                                ,tags = othis.find('input[name="tags"]').val()
                                ,rule = othis.find('input[name="rule"]').val()
                                ,action=othis.find('input[name="action"]').val()
                                ,classty=othis.find('select[name="classty"]').val()
                                ,cid=othis.find('select[name="cid"]').val()
                                ,sortid=othis.find('input[name="sortid"]').val()
                                ,file=othis.find('input[name="file"]').val()
                                ,content=othis.find('input[name="content"]').val()
                                ,pathid=othis.find('input[name="pathid"]').val();
                        if(!tags.replace(/\s/g, '')) return;
                        $.post("/classes/tagssave",{tags:tags,file:file,rule:rule,sortid:sortid,action:action,pathid:pathid,classty:classty,cid:cid,content:content},function (re) {
                            layer.msg(re.msg);
                            setTimeout(function () {
                                // location.reload();
                            },2000);
                        },'json');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });


    layui.use(['tree', 'util'], function() {
        var tree = layui.tree
                , layer = layui.layer
                , util = layui.util

        //基本演示
        var $ = layui.$;
        $.getJSON("/classes/listdata?gid="+gid,function (re) {
            // data=JSON.stringify(re);
            tree.render({
                elem: '#authlist'
                , data: re.data
                , showCheckbox: true  //是否显示复选框
                ,onlyIconControl: true
                , id: 'demoId1'
                , isJump: true //是否允许点击节点时弹出新窗口跳转
                , click: function (obj) {
                    var data = obj.data;  //获取当前点击的节点数据
                    // layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                    // return false;
                    pathid=data.id;
                    if(data.children || data.classty==2 || 1==1){
                        var $btn=['添加子分类','编辑','删除','取消'];
                        var $conf=layer.confirm('选择操作类型？', {
                            btn: $btn //按钮
                            , btn1:function(){
                                layer.close($conf);
                                $('.layuiadmin-btn-tags').click();
                            },btn2:function(){
                                layer.close($conf);
                                editdd();
                            },btn3:function(){
                                layer.close($conf);
                                console.log(123)
                                del();
                            }
                        });
                    }else{
                        var $btn=['编辑','取消','删除'];
                        var $conf=layer.confirm('选择操作类型？', {
                            btn: $btn //按钮
                        }, function(){
                            layer.close($conf);
                            editdd();
                        });
                    }


                    // $('.layuiadmin-btn-tags').click();
                }
            });
        })
        function del(){
            layer.confirm("是否确定删除？",function (){
                $.post("/classes/delClasses",{gid:pathid},function (re) {
                    layer.msg(re.msg);
                    setTimeout(function () {
                        location.reload();
                    },2000);
                },'json');
            })
        }
        function editdd() {
            layer.open({
                type: 2
                ,title: '修改分类'
                ,content: '/classes/tagsform?ty=1&gid='+pathid+'&pathid='+pathid
                ,area: ['450px', '80%']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                            ,tags = othis.find('input[name="tags"]').val()
                            ,rule = othis.find('input[name="rule"]').val()
                            ,cid=othis.find('select[name="cid"]').val()
                            ,sortid=othis.find('input[name="sortid"]').val()
                            ,file=othis.find('input[name="file"]').val()
                            ,content=othis.find('input[name="content"]').val()
                    if(!tags.replace(/\s/g, '')) return;
                    $.post("/classes/authruleedit",{tags:tags,file:file,rule:rule,sortid:sortid,gid:pathid,cid:cid,content:content},function (re) {
                        layer.msg(re.msg);
                        setTimeout(function () {
                            location.reload();
                        },2000);
                    },'json');
                }
            });
        }
        //按钮事件
        util.event('lay-demo', {
            getChecked: function(othis){
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
                // layer.alert(JSON.stringify(checkedData), {shade:0});
                // console.log(checkedData);
                $.post("/adminauth/authgroupsave",{str:JSON.stringify(checkedData),gid:gid},function(re){
                    if(re.code==0){
                        layer.msg("修改成功");
                        setTimeout(function () {
                            history.go(-1);
                        },2000)
                    }
                },"json");
            }
            ,setChecked: function(){
                tree.setChecked('demoId1', [3, 4]); //勾选指定节点
            }
            ,reload: function(){
                //重载实例
                tree.reload('demoId1', {

                });

            }
        });
    });
</script>
</body>
</html>
